package com.example.liqm.greendao.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.liqm.greendao.R;
import com.example.liqm.greendao.db.test.GreenDaoUtils;
import com.example.liqm.greendao.model.DaoMaster;
import com.example.liqm.greendao.model.User;
import com.example.liqm.greendao.model.UserDao;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private UserDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Button button1 = (Button) findViewById(R.id.R_id_btn_c);
       Button button2 = (Button) findViewById(R.id.R_id_btn_r);
       Button button3 = (Button) findViewById(R.id.R_id_btn_u);
       Button button4 = (Button) findViewById(R.id.R_id_btn_d);
        button1.setOnClickListener(l);
        button2.setOnClickListener(l);
        button3.setOnClickListener(l);
        button4.setOnClickListener(l);

        dao = GreenDaoUtils.getSingleTon().getmDaoSession().getUserDao();

    }

    private View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.R_id_btn_c:
                    User insertData = new User(null,"小明", 21,false);
                    dao.insert(insertData);//插入
                    break;
                case R.id.R_id_btn_r:
                    List<User> users = dao.loadAll();//查询
                    for (int i = 0; i <users.size() ; i++) {
                        Log.i("woyaokk","结果："+users.get(i).getName().toString() + "id"+users.get(i).getId());
                    }
                    break;
                case R.id.R_id_btn_u:
                    List<User> userss = dao.loadAll();
                    User user = new User(userss.get(0).getId(),"小红",22,true);
                    dao.update(user);
                    break;
                case R.id.R_id_btn_d:
                    String s = "3";
                    Long l =  Long.parseLong(s);
                    dao.deleteByKey(l);
                    break;



            }
        }
    };
}
